package com.py.credit.controller;

import com.alibaba.fastjson.JSON;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.DynamicSqlService;
import com.py.credit.utils.DateTrans;
import com.py.framework.core.annotation.IgnoreAuthority;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dynamicSql")
public class DynamicSqlController {

    @Autowired
    private DynamicSqlService dynamicSqlService;

    @Value("${tableTitle}")
    private String tableTitle;

    /**
     * 功能描述: 数据修正详情
     *
     * @param data       数据详情
     * @param type       数据类型 I插入 U更新 D删除
     * @param resourceId 表资源id
     * @param recordId   数据值id
     * @param mainName   主体名称
     * @param kId        库Id
     * @return
     * @author YaoS
     * @date 19/5/6 15:02
     */
    @RequestMapping(value = "/operTableInfo.do")
    public RestServiceResult<String> dataRepairInfo(
            @RequestParam(value = "params", defaultValue = "") String data,
            @RequestParam(value = "type", defaultValue = "") String type,
            @RequestParam(value = "resourceId", defaultValue = "") String resourceId,
            @RequestParam(value = "recordId", defaultValue = "") String recordId,
            @RequestParam(value = "mainName", defaultValue = "") String mainName,
            @RequestParam(value = "kId", defaultValue = "") String kId) {
        RestServiceResult<String> r = new RestServiceResult<>();
        Map<String, Object> dataMap = JSON.parseObject(data, HashMap.class);
        try {
            if (type.equals("I")) {
                String message = dynamicSqlService.insertTableInfo(dataMap, resourceId, recordId, mainName, kId);
                if (message != null) {
                    r.setCode(RestServiceResult.CODE_UNKNOW);
                    r.setMsg(message);
                    return r;
                }

            } else if (type.equals("U")) {
                String message = dynamicSqlService.updateTableInfo(dataMap, resourceId, recordId);
                if (message != null) {
                    r.setCode(RestServiceResult.CODE_UNKNOW);
                    r.setMsg(message);
                    return r;
                }

            } else {
                dynamicSqlService.deleteTableInfo(resourceId, recordId);
            }
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg("动态操作表信息失败！");
        }
        return r;
    }

    @IgnoreAuthority
    @RequestMapping(value = "/getTableInfoById.json")
    public RestServiceResult<LinkedHashMap<String, Object>> getTableInfoById(
            @RequestParam(value = "tableId", defaultValue = "") String tableId,
            @RequestParam(value = "id", defaultValue = "") String id,
            @RequestParam(value = "mianName", defaultValue = "") String mianName) {

        RestServiceResult<LinkedHashMap<String, Object>> r = new RestServiceResult<>();
        String ids = tableTitle + tableId;
        try {
            List<LinkedHashMap<String, Object>> dataInfo = dynamicSqlService.selectStyleByTableInfo(ids);
            LinkedHashMap<String, Object> dataValue = dynamicSqlService.selectTableInfoById(ids, id, mianName);
            r.setData(DateTrans.tranDate(dataInfo, dataValue));
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg("动态查询表信息失败！");
        }
        return r;
    }

    @IgnoreAuthority
    @RequestMapping(value = "/getIndexInfoByTybm.json")
    public RestServiceResult<List<LinkedHashMap<String, Object>>> getIndexInfoByTybm(
            @RequestParam(value = "relateSource", defaultValue = "") String relateSource,
            @RequestParam(value = "tybm", defaultValue = "") String tybm,
            @RequestParam(value = "userId", defaultValue = "") String userId
    ) {

        RestServiceResult<List<LinkedHashMap<String, Object>>> r = new RestServiceResult<>();
        try {
            List<LinkedHashMap<String, Object>> params2 = dynamicSqlService.selectIndexInfoByTybm(relateSource, tybm, userId);
            r.setData(params2);
        } catch (Exception e) {
            r.setCode(RestServiceResult.CODE_UNKNOW);
            r.setMsg("动态查询表信息失败！");
        }
        return r;
    }

}
